package com.vcourse.service.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vcourse.cloud.oss.api.feign.OssService;
import com.vcourse.commons.result.R;
import com.vcourse.commons.utils.PageUtils;
import com.vcourse.service.edu.entity.Course;
import com.vcourse.service.edu.entity.Teacher;
import com.vcourse.service.edu.mapper.CourseMapper;
import com.vcourse.service.edu.mapper.TeacherMapper;
import com.vcourse.service.edu.service.TeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vcourse.service.edu.vo.TeacherQueryVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author Zengsh123
 * @since 2020-05-25
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {
    @Resource
    private TeacherMapper teacherMapper;
    @Resource
    private OssService ossService;
    @Resource
    private CourseMapper courseMapper;

    @Override
    public PageUtils queryPage(long currentPage, long limit, TeacherQueryVo queryVo) {
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        // 1、排序
        queryWrapper.orderByAsc("sort");
        // 2、查询
        if (queryVo == null) {
            Page<Teacher> page = this.page(new Page<>(currentPage, limit), queryWrapper);
            return new PageUtils(page);
        }
        String name = queryVo.getName();
        Integer level = queryVo.getLevel();
        String joinDateBegin = queryVo.getJoinDateBegin();
        String joinDateEnd = queryVo.getJoinDateEnd();
        if (StringUtils.isNotBlank(name)) {
            queryWrapper.likeRight("name", name);
        }
        if (level != null) {
            queryWrapper.eq("level", level);
        }
        if (StringUtils.isNotBlank(joinDateBegin)) {
            queryWrapper.ge("join_date", joinDateBegin);
        }
        if (StringUtils.isNotBlank(joinDateEnd)) {
            queryWrapper.le("join_date", joinDateEnd);
        }
        Page<Teacher> page = this.page(new Page<>(currentPage, limit), queryWrapper);
        return new PageUtils(page);
    }

    @Override
    public List<Map<String, Object>> getNames(String name) {
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("name").likeRight("name", name);
        return this.listMaps(queryWrapper);
    }

    @Override
    public boolean deleteAvatarByIds(List<String> ids) {
        List<String> avatarList = teacherMapper.getAvatarByIdList(ids);
        List<String> filePaths = new ArrayList<>();
        // 获取文件的绝对路径，去掉前面的URL地址
        avatarList.forEach(url ->{
            if (StringUtils.isNotBlank(url)) {
                filePaths.add(StringUtils.substringAfter(url, "com/"));
            }
        });
        // 调用OSS服务删除文件
        if (!filePaths.isEmpty()) {
            R res = ossService.deleteFiles(filePaths);
            return res.getSuccess();
        }
        return true;
    }

    @Override
    public Map<String, Object> getTeacherInfo(String id) {
        // 查询讲师
        Teacher teacher = baseMapper.selectById(id);
        // 查询授课信息
        QueryWrapper<Course> courseQueryWrapper = new QueryWrapper<>();
        courseQueryWrapper.eq("teacher_id", id);
        List<Course> courses = courseMapper.selectList(courseQueryWrapper);
        // 封装信息体
        return new HashMap<String, Object>(2) {
            private static final long serialVersionUID = -7968686266729415682L;
            {
            put("teacher", teacher);
            put("courseList", courses);
        }};
    }

    /**
     * 通过缓存注解，自动将数据库内容同步到redis，从redis中取数据
     */
    @Cacheable(value = "siteHome" ,key = "'hotTeacher'")
    @Override
    public List<Teacher> selectHotTeacher() {
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("sort");
        queryWrapper.last("limit 4");
        return baseMapper.selectList(queryWrapper);
    }
}
